package cn.com.chengmandian.bike.controller;

import cn.com.chengmandian.core.redis.cache.AgentParamCacheService;
import cn.com.chengmandian.core.web.pojo.AgentInfo;
import cn.com.chengmandian.core.web.pojo.AjaxResult;
import cn.com.chengmandian.core.web.pojo.BikeParam;
import cn.com.chengmandian.core.web.utils.LocalMapUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.PayScoreService;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.PayScoreServiceImpl;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static cn.com.chengmandian.bike.utils.WxPayUtil.getSerialNumber;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private AgentParamCacheService agentParamCacheService;

    @GetMapping("/cancel")
    public AjaxResult cancel(String no) throws Exception {
        try{
            WxPayService payService = new WxPayServiceImpl();
            //调用初始化config接口
            WxPayConfig payConfig = initialization();
            payService.setConfig(payConfig);
            PayScoreService payScoreService = new PayScoreServiceImpl(payService);
            WxPayScoreResult response = payScoreService.cancelServiceOrder(no, "自行支付");
            log.info("取消支付分记录响应 {}", JSONObject.toJSONString(response));
            return AjaxResult.success(JSONObject.toJSONString(response));
        }catch(Exception e){
            log.error(e.getMessage());
            return AjaxResult.success(e.getMessage());
        }
    }

    @GetMapping("/query")
    public AjaxResult query(String no) throws Exception {
        WxPayService payService = new WxPayServiceImpl();
        //调用初始化config接口
        WxPayConfig payConfig = initialization();
        payService.setConfig(payConfig);
        PayScoreService payScoreService = new PayScoreServiceImpl(payService);
        //查询支付分订单（暂定已创单和订单进行为订单有效）
        WxPayScoreResult response = payScoreService.queryServiceOrder(no, "");
        return AjaxResult.success(JSONObject.toJSONString(response));
    }

    private WxPayConfig initialization(){
        AgentInfo agentInfo = LocalMapUtil.getAgentInfo();
        BikeParam param = agentParamCacheService.getParam(agentInfo.getId());
        //初始化configparam配置
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setMchId(agentInfo.getWxMchId());
        payConfig.setMchKey(agentInfo.getWxApiKey3());
        payConfig.setAppId(agentInfo.getWxMiniAppid());
        payConfig.setServiceId(param.getServiceId());
        payConfig.setApiV3Key(agentInfo.getWxApiKey3());
        payConfig.setPrivateKeyPath(agentInfo.getWxKeyPath());
        payConfig.setPrivateCertPath(agentInfo.getWxCertPath());
        String serialNumber = getSerialNumber(agentInfo.getWxCertPath());
        payConfig.setCertSerialNo(serialNumber);
        log.info("config配置:"+payConfig);
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);
        return payConfig;
    }
}
